package leetcode100;

public class lc058 {

    private int[][] offsets = {
            {1, 0},
            {0, 1},
            {-1, 0},
            {0, -1}};


    public int numIslands(char[][] grid) {

        int row = grid.length;
        int col = grid[0].length;
        int count = 0;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }

    public void dfs(char[][] grid, int i, int j) {
        int row = grid.length;
        int col = grid[0].length;

        if ( i < 0 || i >= row || j < 0 || j >= col||grid[i][j] == '0' ) {
            return;
        }

        grid[i][j] = '0';
        dfs(grid, i + this.offsets[0][0], j+this.offsets[0][1]);
        dfs(grid, i + this.offsets[1][0], j+this.offsets[1][1]);
        dfs(grid, i + this.offsets[2][0], j+this.offsets[2][1]);
        dfs(grid, i + this.offsets[3][0], j+this.offsets[3][1]);
    }
}
